<?php
namespace App\Controllers\Goods;
use App\Libs\Goods\Goods;
use App\Models\Goods\Goods as GoodsModel;
/**
*	获取商品
*/
class GetGoods extends \App\Libs\Base\BaseAction
{
    protected function beforeRun() {
        parent::beforeRun();
        //$this->needLogin = TRUE;
    }
	
	public function execute(){
		$params['key'] = $this->request->get( 'key' );//关键字
		$params['size'] = (int)$this->request->get( 'size' );//展示条数
		$params['page'] = (int)$this->request->get( 'page' );//页数
		$params['lnt'] = $this->request->get( 'lnt' );//经度
		$params['lat'] = $this->request->get( 'lat' );//维度
		$params['order'] = $this->request->get( 'order' );//排序
		$params['nearby'] = (int)$this->request->get( 'nearby' );//附近
		$params['type'] = (int)$this->request->get( 'type' );//类别
		$params['city'] = (int)$this->request->get( 'city' );//城市
		
		if( empty( $params['city'] ) ){
			return \App\Libs\Views\Json::view( 1 , '城市错误，请选择相应的开放城市！', NULL);	
		}
		
		if( empty( $params['lnt'] ) ){
			return \App\Libs\Views\Json::view( 1 , '请选择相应地理位置！', NULL);
		}
		/*if( empty( $params['order'] ) ){
			return \App\Libs\Views\Json::view( 1 , '排序规则不能为空', NULL);
		}*/
		/*if( empty( $params['nearby'] ) && $params['nearby'] != 0 ){
			return \App\Libs\Views\Json::view( 1 , '默认附近值不能为空', NULL);
		}*/
		if( empty( $params['type'] ) && $params['type'] != 0 ){
			return \App\Libs\Views\Json::view( 1 , '默认类别不能为空', NULL);
		}
		if( $params['page'] <= 0 ){
			$params['page'] = 1;
		}
		if( $params['size'] <= 0 ){
			$params['size'] = 10;
		}
		
		$data = Goods::goodsStoreSearch( $params );
		$res = [];
		if( !empty( $data ) ){
			$resultInfo = [];
			$strInParams = [];
			foreach($data as $k=>$v){
				$v['distance'] = \App\Libs\Helper\CommonHelper::transformDis($v['distance']);
				$resultInfo[$v['id']]['base'] = $v;
				$strInParams[] = $v['id'];
			}
			//查找商品
			$strInParamsString = implode(',',$strInParams);//参数
			$goodsInfo = Goods::getGoodsSidIn( $strInParamsString );
			if(!empty($goodsInfo)){
  	                   foreach( $goodsInfo as $k=>$v ){
                    		$resultInfo[$v['sid']]['data'][] = $v;
                	   }
			   foreach ($resultInfo as $sid => $list) {
				if (!isset($list['data']) || empty($list['data'])) {
				    unset($resultInfo[$sid]);
				}
			   }
            		}
		       $resultInfoArr = array_values($resultInfo);	
			return \App\Libs\Views\Json::view(0, '成功', $resultInfoArr);
		}else{
			return \App\Libs\Views\Json::view(0, '未查到信息', []);	
		}
	}
}
